﻿package utils {
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	public class Keyboards {
		private var arrKey:Array;
		private var arrKey2:Array;
		private var arrKeyboard:Array;
		private var type:int = 0;
		private var keystatus:int = 0;
		public var login:MovieClip;
		public var OnNodepress:Class;
		public var datatract:Array;
		
		public function Keyboards(_varmovie:MovieClip, _OnNodepress:Class, _datatract:Array) {
			this.login = _varmovie;
			this.OnNodepress = _OnNodepress;
			this.datatract = _datatract;
		}
		
		public function main():void {
			try {
				arrKey = new Array();
				arrKey[0] = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
				arrKey[1] = new Array("q", "w", "e", "r", "t", "y", "u", "i", "o", "p");
				arrKey[2] = new Array("a", "s", "d", "f", "g", "h", "j", "k", "l");
				arrKey[3] = new Array("z", "x", "c", "v", "b", "n", "m", "-");
				arrKey2 = new Array();
				arrKey2[0] = new Array("!", "@", "#", "$", "%", "^", "&", "*", "(", ")");
				arrKey2[1] = new Array("Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P");
				arrKey2[2] = new Array("A", "S", "D", "F", "G", "H", "J", "K", "L");
				arrKey2[3] = new Array("Z", "X", "C", "V", "B", "N", "M", "_");
				login.login.keyboarding.visible = false;
				regisButtion(login.login.keyboarding.btnDelete);
				regisButtion(login.login.keyboarding.btnShift);
				regisButtion(login.login.keyboarding.btnEnter);
				keystatus = 0;
				configKeyboardVirtual(0);
				login.login.onkeypress.addEventListener(MouseEvent.CLICK, setVisibleVirtualKeyboard);
				login.login.keyboarding.btnDelete.addEventListener(MouseEvent.CLICK, deleteCharInString);
				login.login.keyboarding.btnShift.addEventListener(MouseEvent.CLICK, onClickShift);
				login.login.keyboarding.btnEnter.addEventListener(MouseEvent.CLICK, setVisibleVirtualKeyboard);
				
			} catch (e:Error) {
			}
		}
		
		public function regisButtion(object:MovieClip):void {
			object.stop();
			object.addEventListener(MouseEvent.MOUSE_OVER, functionmouseover);
			object.addEventListener(MouseEvent.MOUSE_OUT, functionmouseout);
			object.addEventListener(MouseEvent.MOUSE_DOWN, functionmousedown);
			object.addEventListener(MouseEvent.MOUSE_UP, functionmouseup);
			object.addEventListener(MouseEvent.MOUSE_MOVE, functionmousemove);
		}
		
		public function functionmouseout(evt:MouseEvent):void {
			evt.currentTarget.gotoAndStop(1);
		}
		
		public function functionmouseover(evt:MouseEvent):void {
			evt.currentTarget.gotoAndStop(2);
		}
		
		public function functionmousedown(evt:MouseEvent):void {
			evt.currentTarget.gotoAndStop(3);
		}
		
		public function functionmouseup(evt:MouseEvent):void {
			evt.currentTarget.gotoAndStop(2);
		}
		
		public function functionmousemove(evt:MouseEvent):void {
			evt.currentTarget.gotoAndStop(2);
		}
		
		private function onClickShift(e:MouseEvent):void {
			if (type == 0) {
				configKeyboardVirtual(0);
			} else if (type == 1) {
				configKeyboardVirtual(1);
			}
		}
		
		private function setVisibleVirtualKeyboard(e:MouseEvent):void {
			if (keystatus == 0) {
				login.login.keyboarding.visible = true;
				keystatus = 1;
			} else if (keystatus == 1) {
				login.login.keyboarding.visible = false;
				keystatus = 0;
			}
		
		}
		
		private function configKeyboardVirtual(t:int):void {
			try {
				var i:int;
				var j:int;
				var mc:MovieClip = null;
				while (login.login.keyboarding.background.numChildren > 0) {
					login.login.keyboarding.background.removeChildAt(login.login.keyboarding.background.numChildren - 1); // for removing top-down
				}
				if (t == 0) {
					arrKeyboard = new Array();
					for (i = 0; i < arrKey.length; i++) {
						for (j = 0; j < arrKey[i].length; j++) {
							mc = new OnNodepress() as MovieClip;
							mc.name = arrKey[i][j];
							mc.node.txtKey.text = arrKey[i][j];
							mc.x = 15 * j + 1;
							mc.y = 15 * i + 1;
							regisButtion(mc.node);
							mc.addEventListener(MouseEvent.CLICK, selectedKey);
							login.login.keyboarding.background.addChild(mc);
						}
					}
					type = 1;
				} else if (t == 1) {
					arrKeyboard = new Array();
					for (i = 0; i < arrKey2.length; i++) {
						for (j = 0; j < arrKey2[i].length; j++) {
							mc = new OnNodepress() as MovieClip;
							mc.name = arrKey2[i][j];
							mc.node.txtKey.text = arrKey2[i][j];
							mc.x = 15 * j + 1;
							mc.y = 15 * i + 1;
							regisButtion(mc.node);
							mc.addEventListener(MouseEvent.CLICK, selectedKey);
							login.login.keyboarding.background.addChild(mc);
						}
					}
					type = 0;
				}
			} catch (err:Error) {
			}
		}
		
		private function selectedKey(e:MouseEvent):void {
			try {
				var str:String = login.login.password.text;
				login.login.password.text = str + e.currentTarget.name;
			} catch (err:Error) {
			}
		}
		
		private function deleteCharInString(e:MouseEvent):void {
			var str:String = login.login.password.text;
			var str2:String = str.substring(0, str.length - 1);
			login.login.password.text = str2;
		}
	}
}
